package com.booth.apply.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.booth.apply.entity.Admin;
import com.booth.apply.entity.Apply;
import com.booth.apply.entity.ApplyLog;
import com.booth.apply.entity.dto.OptionApplyDto;
import com.booth.apply.entity.enums.ApplyStatus;
import com.booth.apply.mapper.ApplyMapper;
import com.booth.apply.service.IAdminService;
import com.booth.apply.service.IApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booth.apply.service.ILogService;
import com.booth.apply.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2020-08-19
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements IApplyService {

    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private ILogService iLogService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonResult agreeApply(OptionApplyDto optionApplyDto) {

        if (iAdminService.list(new QueryWrapper<Admin>().lambda()
                .eq(Admin::getJobNumber, optionApplyDto.getAdminJobNumber())).size() == 0) {
            return CommonResult.failed("管理员不存在");
        }

        Apply apply = new Apply();
        apply.setId(optionApplyDto.getApplyId());
        apply.setFlag(ApplyStatus.AGREE.getCode());
        if (!StringUtils.isEmpty(optionApplyDto.getMessage())) {
            apply.setMessage("同意");
        } else {
            apply.setMessage(optionApplyDto.getMessage());
        }

        if (baseMapper.updateById(apply) == 1) {
            // insert log
            // select user's id
            apply = baseMapper.selectById(optionApplyDto.getApplyId());
            ApplyLog applyLog = new ApplyLog();
            applyLog.setApplyId(optionApplyDto.getApplyId());
            applyLog.setTime(new Date());
            applyLog.setFlag(0);
            applyLog.setAdminJobNumber(optionApplyDto.getAdminJobNumber());
            applyLog.setUserId(apply.getUserId());
            if (iLogService.save(applyLog)) {
                return CommonResult.success("审批成功");
            }
        }

        return CommonResult.success("审批失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonResult refuseApply(OptionApplyDto optionApplyDto) {
        if (iAdminService.list(new QueryWrapper<Admin>().lambda()
                .eq(Admin::getJobNumber, optionApplyDto.getAdminJobNumber())).size() == 0) {
            return CommonResult.failed("管理员不存在");
        }

        Apply apply = new Apply();
        apply.setId(optionApplyDto.getApplyId());
        apply.setFlag(ApplyStatus.AGREE.getCode());
        if (!StringUtils.isEmpty(optionApplyDto.getMessage())) {
            apply.setMessage("拒绝");
        } else {
            apply.setMessage(optionApplyDto.getMessage());
        }

        if (baseMapper.updateById(apply) == 1) {
            // insert log
            // select user's id
            apply = baseMapper.selectById(optionApplyDto.getApplyId());
            ApplyLog applyLog = new ApplyLog();
            applyLog.setApplyId(optionApplyDto.getApplyId());
            applyLog.setTime(new Date());
            applyLog.setFlag(1);
            applyLog.setAdminJobNumber(optionApplyDto.getAdminJobNumber());
            applyLog.setUserId(apply.getUserId());
            if (iLogService.save(applyLog)) {
                return CommonResult.success("审批成功");
            }
        }

        return CommonResult.success("审批失败");
    }
}
